package com.example.musicplayer

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MusicPlayer:Fragment() {

    private val musicData=ArrayList<music>()

    private lateinit var mediaPlayer: MediaPlayer

    private lateinit var spinImage:ImageView

    private lateinit var StartOrPauseBtn:ImageButton

    private lateinit var stopBtn:ImageButton

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view=inflater.inflate(R.layout.music_player,container,false)
        spinImage=view.findViewById(R.id.spinImage)
        StartOrPauseBtn=view.findViewById(R.id.StartOrPauseBtn)
        stopBtn=view.findViewById(R.id.stopBtn)
        mediaPlayer= MediaPlayer()
        return view
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val JSONWithMusicListService=ServiceCreator.create(JSONImageService::class.java)
        JSONWithMusicListService.getListData().enqueue(object : Callback<List<music>> {
            override fun onResponse(call: Call<List<music>>, response: Response<List<music>>) {
                val list=response.body()
                if (list!=null) {
                    for (i in list) {
                        musicData.add(i)
                    }
                }
                Glide.with(this@MusicPlayer)
                    .load(musicData[0].coverImgUrl)
                    .apply(RequestOptions.circleCropTransform())
                    .into(spinImage)
                StartOrPauseBtn.setImageResource(R.drawable.play)
                stopBtn.setImageResource(R.drawable.replay)
                initMediaPlayer(musicData[0].src)
                setupStartOrPauseClickListener()
                setupStopClickListener(musicData[0].src)
            }

            override fun onFailure(p0: Call<List<music>>, p1: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }
    private fun initMediaPlayer(musicUrl:String) {
        mediaPlayer.release()
        mediaPlayer=MediaPlayer.create(requireContext(), Uri.parse(musicUrl))
        StartOrPauseBtn.setImageResource(R.drawable.play)
    }

    private fun setupStartOrPauseClickListener() {
        StartOrPauseBtn.setOnClickListener {
            if (!mediaPlayer.isPlaying) {
                mediaPlayer.start()
                StartOrPauseBtn.setImageResource(R.drawable.pause)
            }
            else {
                mediaPlayer.pause()
                StartOrPauseBtn.setImageResource(R.drawable.play)
            }
        }
    }
    private fun setupStopClickListener(musicUrl:String) {
        stopBtn.setOnClickListener {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
            else if (!mediaPlayer.isPlaying) {
                mediaPlayer.release()
                initMediaPlayer(musicUrl)
            }
        }
    }

}